<?php
/**
 * @return
 * @author 朱乐斌
 * @date
 */

namespace app\admin\controller;

use app\admin\model\UserModel;
use app\BaseController;
use app\validate\UserValidate;
use think\Exception;

class Login extends BaseController
{
    /**
     *  渲染登录页面
    */
    public function index()
    {
        return view('login');
    }

    /**
     *  接收数据验证
    */
    public function checkLogin()
    {
        try {
            // 接收数据
            $data = $this->request->post();
            // 验证数据
            validate(UserValidate::class)->scene('checkLogin')->check($data);
            // 获取数据库信息
            $user = new UserModel();
            if(empty($user->where('username',$data['username'])->find())){
                return json(['msg' => '用户不存在','code' => 400]);
            }else{
                if ($user->where('username',$data['username'])->find()->password === md5($data['password'])){
                    return json(['msg' => '登录成功','code' => 200]);
                }else{
                    return json(['msg' => '密码错误','code' => 400]);
                }
            }
        }catch (Exception $e){
            return json($e->getMessage());
        }


    }
}